package com.weirong.smartvault.backservice.entity_model;

import com.weirong.smartvault.dao.AreaRepository;
import com.weirong.smartvault.dao.DepartmentRepository;
import com.weirong.smartvault.dao.DictRepository;
import com.weirong.smartvault.dao.RouteRepository;
import com.weirong.smartvault.entity.Area;
import com.weirong.smartvault.entity.Department;
import com.weirong.smartvault.entity.Dict;
import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;
import com.weirong.smartvault.entity.Route;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 于2017/11/25由 KFEB4 创建 .
 * 模板层的department数据显示
 */
public class DepartmentModel {
    private Department department;
    private final DictRepository dictRepository;
    private final DepartmentRepository departmentRepository;
    private AreaRepository areaRepository;
    private RouteRepository routeRepository;
    public DepartmentModel(Department department){
        this.department = department;
        this.departmentRepository = SpringContextUtil.getBean(DepartmentRepository.class);
        this.dictRepository = SpringContextUtil.getBean(DictRepository.class);
        this.areaRepository = SpringContextUtil.getBean(AreaRepository.class);
        this.routeRepository = SpringContextUtil.getBean(RouteRepository.class);
    }
    public String getName() {
        return department.getName();
    }

    public String getAddressDetail() {
        Area  area = areaRepository.getOne(department.getRegionId());
        StringBuilder head = new StringBuilder(area.getVal()+department.getAddressDetail());
        for (int i = 0; i <5 ; i++) {
            //当数据库有环的时候，产生无限循环
            String code= area.getParentCode();
            if(code == null)
                break;
            area = areaRepository.getByCode(code);
            head.insert(0, area.getVal());
        }
        return head.toString();
    }
    public String getAddressWithOutVillage(){
        return department.getAddressDetail();
    }
    public Integer getId() {
        return department.getId();
    }

    public String getManage_by() {
        Integer id = department.getManagedBy();
        if (id ==null)
            return  "";
        Department partent = departmentRepository.findOne(id);
        return partent.getName();
    }
    public Integer getManage_byId(){
        return department.getManagedBy();
    }
    public String getType() {
        Dict dict = getTypeDict();
        if(dict==null)
            return "";
        return dict.getName();
    }

    public String getCode(){
        return department.getCode();
    }
    public Dict getTypeDict(){
        String id = department.getType();
        if (id==null || id.length()==0)
            return null;
        Dict partent = dictRepository.findDictByCode("depart_type");
        List<Dict> type = dictRepository.findDictByParentId(partent.getId ());
        for (Dict t :type)
            if (Objects.equals(t.getCode(), department.getType()))
                return t;
        return null;
    }
    public Route getRoute(){
        Integer id = department.getRouteId();
        if (id==null)
            return null;
        Route route= routeRepository.findOne(id);
        return route;
    }
    public String getComment(){
        return department.getComment();
    }
    public Area getProvince(){
        Integer id =department.getRegionId();
        if (id==null){
            return null;
        }
        Area  area = areaRepository.getOne(id);
        while (true) {
            //当数据库有环的时候，产生无限循环
            String code= area.getParentCode();
            if(code == null)
                return area;
            area = areaRepository.getByCode(code);
        }
    }
    public Area getCity(){
        Integer id =department.getRegionId();
        if (id==null){
            return null;
        }
        Area  area = areaRepository.getOne(department.getRegionId());
        Area city=null;
        while (true) {
            //当数据库有环的时候，产生无限循环
            String code= area.getParentCode();
            if(code == null)
                return city;
            city = area;
            area = areaRepository.getByCode(code);
        }
    }
    public Area getVillage(){
        Integer id =department.getRegionId();
        if (id==null){
            return null;
        }
        Area village = areaRepository.findOne(department.getRegionId());
        Area city = getCity();
        if (city==null)
            return null;
        if (Objects.equals(village.getParentCode(), city.getCode()))
            return village;//近当当前id是city的子类才有效
        return null;
    }
    public Department getSource(){
        return department;
    }
    public Department getSuperiorDepartment(){
        Integer id  =department.getManagedBy();
        if (id==null)
            return null;
        return departmentRepository.findOne(id);
    }
    public List<DepartmentModel> getAllDepartments(){
        List<DepartmentModel> res = getAllSubDepartment();
        res.add(this);
        return res;
    }
    public List<DepartmentModel> getAllSubDepartment() {
        List<DepartmentModel> res = new ArrayList<>();
        List<Department> departments = departmentRepository.findAllByManagedBy(getId());
        while(departments.size()>0){//循环结构的机构引用导致死循环
            Department d = departments.get(0);
            departments.addAll(departmentRepository.findAllByManagedBy(d.getId()));
            departments.remove(0);
            res.add(new DepartmentModel(d));
        }
        return res;
    }
}
